package collections;

import java.util.HashMap;

/**
 * Created by pc on 2015/1/20.
 * Hashtable判断两个value相等的情况是，两个value对象equals比较结果相等，因为有个B1对象与谁都相等，所以containsValue总是返回true*
 */
class A1 {
    int count;
    public A1(int count) {
        this.count = count;
    }
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj != null && obj.getClass() == A1.class) {
            A1 a = (A1) obj;
            return this.count == a.count;
        }
        return false;
    }
    public int hashCode() {
        return this.count;
    }
}

class B1 {
    public boolean equals(Object obj) {
        return true;
    }
}

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap hm = new HashMap();

//        Hashtable hm = new Hashtable();
        hm.put(new A1(1), "Hello");
        hm.put(new A1(2), "Good");
        hm.put(new A1(3), new B1());
        System.out.println(hm);
        
        System.out.println("because of B1 and Hashtable , always true: " + hm.containsValue("北京天安门"));
        System.out.println("because of B1 and Hashtable , always true: " + hm.containsValue(new B1()));
        System.out.println("because of A1 hashcode, true: " + hm.containsKey(new A1(2)));
        System.out.println(new A1(5).equals(new A1(5)));

    }

}
